﻿using CommonLibrary.Models.ControllerStates;
using CommonLibrary.Models.ControllerStates.PS3;
using ConnectionManager.Extensions;

namespace ConnectionManager.ConnectionProcesors.PS3
{
    /// <summary>
    /// Default PS3 procesor 
    /// Values of the rstick are changed to null if the input dose not change in the default SentTimes
    /// </summary>
    class DefaultPS3Procesor : IConnectionProcesor
    {
        private static ushort _rAxisX;
        private static ushort _rAxisY;

        private static int _countx;
        private static int _county;
        private const int SentTimes = 5;

        public void Process(ControllerState controllerState)
        {
            PrecheckCursor(controllerState as PS3ControllerState);
        }

        public string GetName()
        {
            return "Default";
        }


        private static void PrecheckCursor(PS3ControllerState dataStructure)
        {
            if (dataStructure.User.RAxisX == _rAxisX && dataStructure.User.RAxisX.NotCloseToMax())
            {
                _countx++;
            }
            else
            {
                _rAxisX = dataStructure.User.RAxisX;
                _countx = 0;
            }

            if (dataStructure.User.RAxisY == _rAxisY && dataStructure.User.RAxisY.NotCloseToMax())
            {
                _county++;
            }
            else
            {
                _rAxisY = dataStructure.User.RAxisY;
                _county = 0;
            }

            if (_countx >= SentTimes)
            {
                dataStructure.User.RAxisX = ushort.MaxValue / 2;
                _rAxisX = dataStructure.User.RAxisX;
            }

            if (_county >= SentTimes)
            {
                dataStructure.User.RAxisY = ushort.MaxValue / 2;
                _rAxisY = dataStructure.User.RAxisY;
            }
        }

        private static void ResetButtonData(PS3ControllerState dataStructure)
        {
            dataStructure.User.Buttons.Circle.Pressed = false;
            dataStructure.User.Buttons.Circle.Value = 0;
            dataStructure.User.Buttons.Cross.Pressed = false;
            dataStructure.User.Buttons.Cross.Value = 0;
            dataStructure.User.Buttons.Down.Pressed = false;
            dataStructure.User.Buttons.Down.Value = 0;
            dataStructure.User.Buttons.L1.Pressed = false;
            dataStructure.User.Buttons.L1.Value = 0;
            dataStructure.User.Buttons.L2.Pressed = false;
            dataStructure.User.Buttons.L2.Value = 0;
            dataStructure.User.Buttons.L3.Pressed = false;
            dataStructure.User.Buttons.L3.Value = 0;
            dataStructure.User.Buttons.Left.Pressed = false;
            dataStructure.User.Buttons.Left.Value = 0;
            dataStructure.User.Buttons.PS.Pressed = false;
            dataStructure.User.Buttons.PS.Value = 0;
            dataStructure.User.Buttons.R1.Pressed = false;
            dataStructure.User.Buttons.R1.Value = 0;
            dataStructure.User.Buttons.R2.Pressed = false;
            dataStructure.User.Buttons.R2.Value = 0;
            dataStructure.User.Buttons.R3.Pressed = false;
            dataStructure.User.Buttons.R3.Value = 0;
            dataStructure.User.Buttons.Right.Pressed = false;
            dataStructure.User.Buttons.Right.Value = 0;
            dataStructure.User.Buttons.Select.Pressed = false;
            dataStructure.User.Buttons.Select.Value = 0;
            dataStructure.User.Buttons.Square.Pressed = false;
            dataStructure.User.Buttons.Square.Value = 0;
            dataStructure.User.Buttons.Start.Pressed = false;
            dataStructure.User.Buttons.Start.Value = 0;
            dataStructure.User.Buttons.Triangle.Pressed = false;
            dataStructure.User.Buttons.Triangle.Value = 0;
            dataStructure.User.Buttons.Up.Pressed = false;
            dataStructure.User.Buttons.Up.Value = 0;
        }
    }
}
